More events in Asp.Net

Okay, the last event of the last chapter was easy, but try to make all the code necessary to use an event from the beginning. We can still add a new control to create more interesting - dropdown list, allowing users to select an item from a list. Add default snapet to default .aspx file:

<asp:DropDownList runat="server" id="GreetList" autopostback="true">
    <asp:ListItem value="no one">No one</asp:ListItem>
    <asp:ListItem value="world">World</asp:ListItem>
    <asp:ListItem value="universe">Universe</asp:ListItem>
</asp:DropDownList>

This thing works like a normal HTML selection element, which is definitely translated into translation. The only feature that appears to be new to someone with basic HTML experience is autopostback, you can postback in one of the next chapters You will learn more about, but for now, just know that it controls everyone on the server once the item is selected by the user by adding an event, we Sector will no longer use it to our advantage:

<asp:DropDownList runat="server" id="GreetList" autopostback="true" onselectedindexchanged="GreetList_SelectedIndexChanged">

We are using obsolete end-embedded events, and specify a method from codebind that has not yet existed. You are free to choose the name of the method, but using a conference on the name of control, an underscore, and then the name of the event, you can help keep track of it all. We create better events, so change to Default.aspx.cs file, and add the following method:

protected void GreetList_SelectedIndexChanged(object sender, EventArgs e)
{
    HelloWorldLabel.Text = "Hello, " + GreetList.SelectedValue;
}

Once again, we make it very simple. We use the selected value property of our dropdown list, in which the selected item has the value property text. Try to run the site, and select an item from the dropdown list. Pretty clean, is not it? All commen controls come with a bunch of useful events, which you can subscribe in this way.